const {Bednumber} = require("../models/Bednumber") // 床位id
const {RoomNo} = require("../models/RoomNo") // 房间表
const {Floor} = require("../models/Floor") // 楼层表
const {Dormitory} = require("../models/Dormitory") // 宿舍楼表
const {Sequelize} = require("sequelize")

/**
 * 修改以住人数
 * @param {Object} idObj id对象
 * @param {*} idObj.roomNoId 房间id
 * @param {*} idObj.floorID 楼层id
 * @param {*} idObj.dormitoryId 宿舍楼id
 * @param {*} idObj.bednumberId 床号id
 * @param {Number} isPeople true：入住 false：退宿
 * @returns 返回修改的影响行数
 */
exports.updataToStay = async function(idObj,isPeople){
    const count = isPeople ? 1 : -1
    try{
        // 修改床位表中的isPeople(入住和退宿)
        const ins = await Bednumber.update({isPeople},{
            where:{
                id:idObj.bednumberId
            }
        })
        // 修改宿舍表中的以住人数
        const RoomNoStay = await RoomNo.update({roomResided:Sequelize.literal(`roomResided + ${count}`)},{
            where:{
                id: idObj.roomNoId
            }
        })
        // 修改楼层中的以住人数
        const floorIDStay = await Floor.update({floorResided: Sequelize.literal(`floorResided + ${count}`)},{
            where:{
                id:idObj.floorID
            }
        })
        // 修改宿舍楼的以住人数
        const DormitoryStay = await Dormitory.update({dormitoryresided: Sequelize.literal(`dormitoryresided + ${count}`)},{
            where:{
                id:idObj.dormitoryId
            }
        })
        return {
            ins,
            RoomNoStay,
            floorIDStay,
            DormitoryStay
        }
    }catch(err){
        return err
    }
    
}